package com.imtimmy.android.pda;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ActivityItemAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<ActivityInfo> items;
	private HashMap<Integer, SoftReference<Bitmap>> imageCache;

	public ActivityItemAdapter(Context ctx) {
		mInflater = LayoutInflater.from(ctx);
		items = new ArrayList<ActivityInfo>();
		imageCache = new HashMap<Integer, SoftReference<Bitmap>>();
	}

	public void AddData(ArrayList<ActivityInfo> mList) {
		items = mList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		ActivityInfo item = items.get(position);

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.activitylist_item, null);

			holder.background = (LinearLayout) convertView
					.findViewById(R.id.item_background);
			holder.iv_image = (ImageView) convertView
					.findViewById(R.id.imageView_Pic_activitylistitem);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_Title_activitylistitem);
			holder.tv_content = (TextView) convertView
					.findViewById(R.id.tv_Content_activitylistitem);
			holder.tv_timestamp = (TextView) convertView
					.findViewById(R.id.tv_Timestamp_activitylistitem);
			holder.iv_hasAudio = (ImageView) convertView
					.findViewById(R.id.iv_hasAudio);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.background.setBackgroundColor(item.getColor());
		if (item.getAudio().length() > 0) {
			holder.iv_hasAudio.setVisibility(View.VISIBLE);
		} else {
			holder.iv_hasAudio.setVisibility(View.GONE);
		}

		SoftReference<Bitmap> sReference = imageCache.get(item.getId());
		Bitmap image = null;
		if (sReference != null) {
			image = sReference.get();
		}
		if (image == null) {
			try {
				if (item.getImage().length() > 0) {
					Log.i("create drawable", "create drawable");
					image = getPhotoItem(item.getImage(), 4);
					imageCache.put(item.getId(), new SoftReference<Bitmap>(
							image));
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		if (image != null) {
			if (!image.isRecycled()) {
				holder.iv_image.setImageBitmap(image);
				// image.recycle();
			}
			holder.iv_image.setVisibility(View.VISIBLE);
		} else {
			holder.iv_image.setVisibility(View.GONE);
		}

		holder.tv_title.setText(item.getTitle());
		holder.tv_content.setText(item.getContent());
		Date date = new Date(item.getDate());
		StringBuilder time = new StringBuilder().append(date.getYear() + 1900)
				.append("-").append(date.getMonth() + 1).append("-")
				.append(date.getDate()).append(" ")
				.append(pad(date.getHours())).append(":")
				.append(pad(date.getMinutes()));
		holder.tv_timestamp.setText(time);

		return convertView;
	}

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	public Bitmap getPhotoItem(String filepath, int size) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = size;
		Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);
		bitmap = Bitmap.createScaledBitmap(bitmap, 150, 100, true);//
		// 预先缩放，避免实时缩放，可以提高更新率
		return bitmap;
	}
}

class ViewHolder {
	public TextView tv_title;
	public TextView tv_content;
	public TextView tv_timestamp;
	public ImageView iv_image;
	public ImageView iv_hasAudio;
	public LinearLayout background;
}
